//
//  LCAttributeCell.m
//  webSocket
//
//  Created by 奇艺果 on 2018/1/26.
//  Copyright © 2018年 奇艺果. All rights reserved.
//

#import "LCAttributeCell.h"
#import "YYKit.h"
#import "NSString+Utility.h"

@interface LCAttributeCell()
{
    UIView *_view;
    UIFont *_textFont;
}
@property (nonatomic, strong) NSString *uName;
@property (nonatomic, strong) NSString *uIcon;
@property (nonatomic, strong) NSString *pName;
@property (nonatomic, strong) NSString *pIcon;
@property (nonatomic, strong) NSString *text;

@property (nonatomic, strong) YYLabel *label;

@end

@implementation LCAttributeCell

- (void)attributeCellWithUName:(NSString *)uName uIcon:(NSString *)uIcon pName:(NSString *)pName pIcon:(NSString *)pIcon text:(NSString *)text{
    _uName = uName;
    _uIcon = uIcon;
    _pName = pName;
    _pIcon = pIcon;
    _text = text;
    self.label.attributedText = [self textAttributedScrollViewWithUName:uName uIcon:uIcon pName:pName pIcon:pIcon text:text];
    [self.label sizeToFit];
    self.label.frame = CGRectMake(20, 0, self.width - 20, self.height);
}
- (NSMutableAttributedString  *)textAttributedScrollViewWithUName:(NSString *)uName uIcon:(NSString *)uIcon pName:(NSString *)pName pIcon:(NSString *)pIcon text:(NSString *)text{
    uName = uName.length ? uName : @"";
    pName = pName.length ? pName : @"";
    if([uName textLength] > 10){
        uName = [uName subStringByByteWithIndex:8];
        uName = [uName stringByAppendingString:@"..."];
    }
    if([pName textLength] > 10){
        pName = [pName subStringByByteWithIndex:8];
        pName = [pName stringByAppendingString:@"..."];
    }
    NSString *ower = [NSString stringWithFormat:@"恭喜  %@ 抓到了  %@ !%@",uName,pName,text];
    NSMutableAttributedString *owerText = [[NSMutableAttributedString alloc] initWithString:ower];
    
    [owerText setAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:@"999999"]}];
    UIImageView *imageview = [UIImageView new];
    [imageview setImageURL:[NSURL URLWithString:uIcon]];
    CGFloat fontsize = (20);
    imageview.size = CGSizeMake(fontsize, fontsize);
    imageview.layer.cornerRadius = fontsize/2;
    imageview.layer.masksToBounds = YES;
    NSMutableAttributedString *attchment1 = [NSMutableAttributedString attachmentStringWithContent:imageview contentMode:UIViewContentModeCenter attachmentSize:imageview.size alignToFont:[UIFont systemFontOfSize:fontsize] alignment:YYTextVerticalAlignmentBottom];
    [owerText replaceCharactersInRange:NSMakeRange(3, 0) withAttributedString:attchment1];
    
    UIImageView *imageview1 = [UIImageView new];
    [imageview1 setImageURL:[NSURL URLWithString:pIcon]];
    imageview1.size = CGSizeMake(fontsize, fontsize);
    imageview1.layer.cornerRadius = fontsize/2;
    imageview1.layer.masksToBounds = YES;
    NSMutableAttributedString *attchment2 = [NSMutableAttributedString attachmentStringWithContent:imageview1 contentMode:UIViewContentModeCenter attachmentSize:imageview1.size alignToFont:[UIFont systemFontOfSize:fontsize] alignment:YYTextVerticalAlignmentBottom];
    
    NSRange nickRange1 = [owerText.string rangeOfString:uName];
    [owerText replaceCharactersInRange:NSMakeRange(nickRange1.length + 10, 0) withAttributedString:attchment2];
    
    NSRange nickRange = [owerText.string rangeOfString:pName];
    [owerText setAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:@"ffb52b"]} range:nickRange1];
    [owerText setAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:@"ffb52b"]} range:nickRange];
    
    return owerText;
    
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        _textFont = ScaleFont(10);
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.label];
    }
    return self;
}

- (YYLabel *)label{
    if(!_label){
        _label = [YYLabel new];
    }
    return _label;
}

@end
